import 'package:flutter/material.dart';
import 'RefreshWidget.dart';

enum PullRefreshStatus { NONE, DRAG, LOADING, RELEASE, LOAD_FINISH }

class BaseRefreshWidget extends StatefulWidget {
  final BaseRefreshState refreshState;

  final ListView child;
  final RefreshCallback onRefresh;

  BaseRefreshWidget({Key key, this.child, this.onRefresh, this.refreshState})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return refreshState != null ? refreshState : new RefreshState();
  }
}

abstract class BaseRefreshState extends State<BaseRefreshWidget> {
  PullRefreshStatus refreshStatus = PullRefreshStatus.NONE;
  var refreshText = "下拉刷新";

  ///释放刷新的临界点
  double maxHeight = 200;
  final double maxPullHeight = 400;
  final double movePara = 2;

  double mLastY = 0;
  double mLastX = 0;
  double dy = 0;
  double dx = 0;
  double _offsetY = 0;

  //当前的y
  double scrollY = 0;

  ///刷新的高度变化
  double dragHeight = 40;
  bool isNeedMyMove = false;

  double _startMetricsPosition = 0.0;
  double _startDragPosition = 0.0;
  double _currentDragPosition = 0.0;
  ScrollController controller;
  BaseRefreshState() {
    controller = ScrollController();
  }

  /// 结束刷新
  finishRefresh() {
    setState(() {
      refreshStatus = PullRefreshStatus.LOAD_FINISH;
    });
  }

  bool onScrollNotificated(ScrollUpdateNotification notification) {
    if (notification.metrics.extentBefore >= dragHeight - 10) {
      setState(() {
        ignore = false;
      });
    }

    print(
        "onScrollNotificated${notification.metrics.extentBefore}======${notification.dragDetails.globalPosition.dy}");
    return true;
  }

  bool onListNotificated(ScrollUpdateNotification notification) {
    if (notification.metrics.extentBefore == 0) {
      setState(() {
        ignore = true;
      });
    }

    print(
        "onListNotificated${notification.metrics.extentBefore}======${notification.dragDetails.globalPosition.dy}");
    return true;
  }

  bool ignore = true;
  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;
    return NotificationListener(
        onNotification: onScrollNotificated,
        child: CustomScrollView(
          shrinkWrap: true,
          controller: controller,
          // 内容
          slivers: <Widget>[
            new SliverPadding(
              padding: const EdgeInsets.only(top: 0),
              sliver: new SliverList(
                delegate: new SliverChildListDelegate(
                  <Widget>[
                    Container(
                        padding: EdgeInsets.only(top:0),
                        height: dragHeight,
                        width: width,
                        child:
                            getRefreshWidget(context, PullRefreshStatus.NONE)),
                    Container(
                        width: width,
                        height: height,
                        child: IgnorePointer(
                            ignoring: ignore,
                            child: NotificationListener(
                                onNotification: onListNotificated,
                                child: widget.child)))
                  ],
                ),
              ),
            ),
          ],
        ));
  }

  Widget getRefreshWidget(
      BuildContext context, PullRefreshStatus refreshStatus);
}

// CustomPaint(
//           size: Size.fromHeight(-100),
//             painter: ProgressPainter(),
//             child: RepaintBoundary(child: Container(child: widget.child))));
//   }

class ProgressPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    print("size===$size");
    // 绘制代码
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}
